import sys 
from UI.login_ui import Ui_MainWindow as Login
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets
from database.database import DataBase
from business.register import Main_Ui as Reg
from PyQt5.QtWidgets import QMessageBox
from business.function_page import Main_Ui as Fun

class Main_Ui(QMainWindow, Login):
    def __init__(self, db_path="./database/plave_data.db"):
        super(Main_Ui, self).__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.login)
        self.database = DataBase(db_path)
        self.pushButton_3.clicked.connect(self._register)
        self.pushButton_2.clicked.connect(self.close)
        self.register = Reg(db_path)
        self.uid = 0
        self.db_path = db_path
        self.function = None

    def login(self):
        password = self.lineEdit.text()
        username = self.lineEdit_2.text()
        try:
            mark, user = self.database.login(username, password)
            if mark:
                    print("登录成功")
                    self.uid = user[0][0]
                    self.close()
                    self.function = Fun(model_path="./plate_detect/ultralytics/weights/best.pt", db_path = self.db_path, uid=self.uid)
                    self.function.show()
            else:
                    print("登录失败")
                    select = QMessageBox.warning(None, '消息','用户名或密码错误', QMessageBox.Yes)

        except Exception as e:
            print(e)
            print("数据查询错误！")

    def _register(self):
        print("注册")
        self.register.show()    

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = Main_Ui()
    ui.show()
    sys.exit(app.exec_())
